﻿using System;
using System.Data;
using System.Linq;
using org.se.fs.presenter.IView.Profile;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.Task.Context;
using org.se.fs.presenter.Task.Template;

namespace org.se.fs.presenter.View.Profile
{
    public class InstitutionSubMenu : Template.SubMenu
    {
        private readonly IInstitutionSubMenu _view;
        private readonly Crud _taskBranch;
        private DataTable _branch;

        public InstitutionSubMenu(IInstitutionSubMenu view) : this(view, new Institution()) { }

        public InstitutionSubMenu(IInstitutionSubMenu view, Crud task)
        {
            _view = view;
            Task = task;
            FormView = _view;
            Name = "Branch";
            _taskBranch = new Branch();
        }

        public override void Initialize()
        {
            base.Initialize();
            _taskBranch.OnGetActive += TaskOnGetActive;
            _taskBranch.GetActive();
        }

        void TaskOnGetActive(object source, FormEvent e)
        {
            if(!e.IsSuccess)
                return;

            _branch = (DataTable) e.Value;
            var a = (_branch.AsEnumerable().Select(b => b.Field<string>("name"))).ToArray();
            _view.FillBranch(a);

            if (IsNewData())
                return;

            var index = GetIndexByDataTable(_branch, "id", Data["branch_id"]);
            _view.Index = index == null ? -1 : (int)index;

        }

        protected override void SetUi()
        {
            _view.FullName = Data["name"].ToString();
            _view.Code = Data["code"].ToString();
            _view.Address = Data["address"].ToString();
            _view.Fax = Data["fax_num"].ToString();
            _view.Mobile = Data["mobile_num"].ToString();
            _view.Tel = Data["tel_num"].ToString();
            _view.ContactName = Data["contact_name"].ToString();
            _view.ContactPosition = Data["contact_position"].ToString();
            _view.ContactMobile = Data["contact_mobile_num"].ToString();
            _view.ContactTel = Data["contact_tel_num"].ToString();
        }

        protected override void FillData()
        {
            Data["name"] = _view.FullName;
            Data["code"] = _view.Code;
            Data["address"] = _view.Address;
            Data["fax_num"] = _view.Fax;
            Data["mobile_num"] = _view.Mobile;
            Data["tel_num"] = _view.Tel;
            Data["contact_name"] = _view.ContactName;
            Data["contact_position"] = _view.ContactPosition;
            Data["contact_mobile_num"] = _view.ContactMobile;
            Data["contact_tel_num"] = _view.ContactTel;
            Data["branch_id"] = Convert.ToUInt64(_branch.Rows[_view.Index]["id"].ToString());
            Data["branch_name"] = _branch.Rows[_view.Index]["name"].ToString();
            Data["branch_code"] = _branch.Rows[_view.Index]["code"].ToString();
            Data["user_create_id"] = ToUInt64(Config.User["id"]);
        }

        protected override bool Validate()
        {
            if (string.IsNullOrEmpty(_view.FullName.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty name.") });
                _view.FocusFullName();
                return false;
            }

            if (string.IsNullOrEmpty(_view.Code.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty code.") });
                _view.FocusCode();
                return false;
            }

            if (_view.Index.Equals(-1))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty branch.") });
                _view.FocusBranch();
                return false;
            }

            TriggerValidate(new FormEvent(true));
            return true;
        }
    }
}
